import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';

import '../../../resources/colours.dart';
import '../../../server/api/other_api.dart';
import '../../../widgets/base/qkd_text.dart';

class KingRuleItem extends StatefulWidget {
  const KingRuleItem({super.key, required this.title, required this.button});

  final String title;
  final String button;

  @override
  State<KingRuleItem> createState() => _KingRuleItemState();
}

class _KingRuleItemState extends State<KingRuleItem> {
  String? rule;
  @override
  void initState() {
    getRule();
    super.initState();
  }

  Future getRule() async{
    var tips = await OtherApi.getAppList("app_res_tips");
    try {
      rule = tips
          ?.firstWhere((element) => element.button == widget.button)
          .content;
    }catch(e){
      log("no rule");
    }
    update();
  }

  @override
  Widget build(BuildContext context) {
    return rule.noValue?Container():Container(
        width: 66,height: 25,
        alignment: Alignment.center,
        decoration: const BoxDecoration(
            borderRadius: BorderRadius.horizontal(left: Radius.circular(15)),
            gradient: LinearGradient(
                colors: [Colours.goldFFF7BD,Colours.goldFFB444])
        ),
        child: QkdText("活动规则", fontSize: 12)
    ).tap(() => Get.bottomSheet(rules(),isScrollControlled: true));
  }

  Widget rules(){
    return BottomSheet(
      onClosing: (){},
      enableDrag: false,
      // constraints: BoxConstraints(maxHeight: Get.height*0.6),
      backgroundColor: Colors.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(8))),
      builder: (context) => Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Image.asset(
                  width: 24, Utils.getImgPath("bottomsheet_close.png")
                ).tap(Get.back),
                QkdText(widget.title,
                  fontSize: 16,
                  fontWeight: FontWeight.w500
                ),
                Container(width: 24)
              ],
            ),
            Container(height: 12),
            QkdText(rule?.split("").join("\u200A") ?? "",
              letterSpacing: -1,
              color: Colours.grey66,
              height: 1.6,
              textAlign: TextAlign.justify,
            ),
            Container(height: 50)
          ],
        ),
      )
    );
  }
}
